@extends('admin.common.main')

@section('content')
    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 角色中心 <span
            class="c-gray en">&gt;</span> 角色管理 <a class="btn btn-success radius r"
                                                      style="line-height:1.6em;margin-top:3px"
                                                      href="javascript:location.replace(location.href);" title="刷新"><i
                class="Hui-iconfont">&#xe68f;</i></a></nav>
    <div class="page-container">
        <form action="{{route('admin.role.index')}}" method="get">
            <div class="text-c"> 角色名称：
                <input type="text" class="input-text" style="width:250px" placeholder="输入角色名称" id=""
                       name="name">
                <button type="submit" class="btn btn-success radius" id="" name=""><i class="Hui-iconfont">&#xe665;</i> 搜角色
                </button>
            </div>
        </form>
        <div class="cl pd-5 bg-1 bk-gray mt-20"><span class="l"><a href="javascript:;" onclick="datadel()"
                                                                   class="btn btn-danger radius"><i
                        class="Hui-iconfont">&#xe6e2;</i> 批量删除</a>
                <a href="{{route('admin.role.create')}}" class="btn btn-primary radius"><i
                        class="Hui-iconfont">&#xe600;</i> 添加角色</a></span>
        </div>
        <div class="mt-20">
            <table class="table table-border table-bordered table-hover table-bg table-sort">
                <thead>
                <tr class="text-c">
                    <th width="25"><input type="checkbox"></th>
                    <th width="80">ID</th>
                    <th width="100">角色名</th>
                    <th width="130">加入时间</th>
                    <th width="100">操作</th>
                </tr>
                </thead>
                <tbody>
                @foreach($data as $key=>$val)
                    <tr class="text-c">
                        <td><input type="checkbox" value="{{$val['id']}}" name="id[]"></td>

                        <td>{{$val['id']}}</td>
                        <td>{{$val['name']}}</td>
                        <td>{{$val['created_at']}}</td>
                        <td class="td-manage">
                            <a title="分配权限" href="{{route('admin.role.node',$val)}}" class="label label-warning radius">分配权限</a>
                            <a title="编辑" href="{{route('admin.role.edit',['id'=>$val['id']])}}" class="label label-primary radius">编辑</a>
                            <a title="删除" href="{{route('admin.role.destroy',$val)}}"
                               class="label label-danger radius delbtn">删除</a>
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>

            {{$data->links()}}
        </div>

        @endsection

        @section('js')
            <script type="text/javascript">

                /*用户-删除*/
                $('.delbtn').click(function (obj) {
                    let url = $(this).attr('href');
                    let _that = $(this);
                    layer.confirm('确认要删除吗？', function () {
                        $.ajax({
                            type: 'DELETE',
                            data: {
                                //这个token标识是_token
                                _token: "{{csrf_token()}}"
                            },
                            url: url,
                            dataType: 'json',
                            success: function (data) {
                                if (data.error_code == 0) {
                                    //删除成功移除当前行
                                    // _that.parents("tr").remove();
                                    //不移除当前行了，要展示恢复按钮
                                    history.go(0)
                                    //提示信息
                                    layer.msg('已删除!', {icon: 1, time: 1000});
                                } else {
                                    layer.msg(data.msg);
                                }

                            },
                            error: function (data) {
                                console.log(data.msg);
                            },
                        });
                    });
                    return false;

                });

                $('.restorebtn').click(function (obj) {
                    let url = $(this).attr('href');
                    $.ajax({
                        type: 'GET',
                        url: url,
                        dataType: 'json',
                        success: function (data) {
                            if (data.error_code == 0) {
                                history.go(0)
                                //提示信息
                                layer.msg('已恢复!', {icon: 1, time: 1000});
                            } else {
                                layer.msg(data.msg);
                            }

                        },
                        error: function (data) {
                            console.log(data.msg);
                        },
                    });

                    return false;

                });


                /**
                 * 批量删除
                 */
                function datadel() {


                    layer.confirm('您确定要批量删除吗？？', function () {

                        let ids = $('input[name="id[]"]:checked')
                        //存储要删除的id
                        let id = []

                        $.each(ids, (key, val) => {
                            //取dom节点value的值，取到要删除id,把要删除的数据追加打数组中
                            id.push(val.value)
                        })

                        $.ajax({
                            type: 'DELETE',
                            data: {
                                //这个token标识是_token
                                _token: "{{csrf_token()}}",
                                id: id
                            },
                            url: "{{route('admin.user.del')}}",
                            dataType: 'json',
                            success: function (data) {
                                if (data.error_code == 0) {
                                    //删除成功移除当前行
                                    // _that.parents("tr").remove();
                                    //不移除当前行了，要展示恢复按钮
                                    history.go(0)
                                    //提示信息
                                    layer.msg('已删除!', {icon: 1, time: 1000});
                                } else {
                                    layer.msg(data.msg);
                                }

                            },
                            error: function (data) {
                                console.log(data.msg);
                            },
                        });
                    });
                    return false;
                }
            </script>
@endsection
